<?php

namespace app\admin\controller\member;

use app\admin\model\UM as UM;
use app\admin\service\LawyerService;
use app\admin\service\LegalService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use ShidianUtil\Utils\CheckUtil;
use think\APP;
use think\exception\ValidateException;
use think\Request as Request;

/**
 * Legal 控制器
 * Class Legal
 * @package app\admin\controller\member;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\admin\validate\member\Legal
 */
class Legal
{
    // 请求参数
    private Request $request;

    // service
    private LawyerService $service;

    public function __construct(Request $request, LawyerService $legalService)
    {
        $this->request = $request;
        $this->service = $legalService;
    }

    /**
     * 列表
     * @param UM|null $user
     * @return ApiResponse
     * @url /member/legal/index
     */
    public function index(?UM $user): ApiResponse
    {
        $request = $this->request;

        $followedWechat = $request->param('followedWechat');
        $medal = $request->param('medal', null);
        $provinceId = $request->param('provinceId', null);
        $cityId = $request->param('cityId', null);
        $startTime = $request->param('startTime', null);
        $endTime = $request->param('endTime', null);
        $kw = $request->param('kw', null);
        $svip = $request->param('svip', null);
        $svipAgree = $request->param('svipAgree');

        $page = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        return ApiResponse::success($this->service->index($user, $followedWechat, $medal, $provinceId, $cityId, $startTime, $endTime,
            $kw, $svip, $svipAgree, 1, $page, $page_size));
    }

    /**
     * 详情
     * @param UM|null $user
     * @return ApiResponse
     * @url /member/legal/read
     */
    public function read(?UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');

        if (!CheckUtil::isId($id)) {
            return ApiResponse::fail('缺少id参数');
        }

        return ApiResponse::success($this->service->read($user, $id));
    }

    /**
     * 新增|修改
     * @param UM $user
     * @return ApiResponse
     * @url /member/legal/save
     */
    public function save(UM $user): ApiResponse
    {
        $request = $this->request;

        if (!$request->has('id')) {
            throw new ValidateException('请选择一名法务');
        }

        $data = $this->service->modify($user, $request->param('id'), $request->param());

        return ApiResponse::success($data);
    }

}
